package geppetto.phraseHMM.phraseExtraction;

import geppetto.phraseTable.PhraseTable;
import geppetto.phraseTable.phrase.Phrase;
import gnu.trove.TIntObjectHashMap;

public class HistogramGlobalPrunning extends AbstractGlobalTablePruning{
	
	double _thresh;
	
	public HistogramGlobalPrunning(double thresh){
		_thresh = thresh;
	}
	
	public void pruneTable(PhraseTable pt){
		for(int targetLen =0; targetLen  < pt._maxTargetPhraseLen; targetLen++){
			for(int sourceLen =0; sourceLen  < pt._maxSourcePhraseLen; sourceLen++){
				TIntObjectHashMap<TIntObjectHashMap<Phrase>> phrasesT = pt._phraseTable[sourceLen][targetLen];
				int keysT[] = phrasesT.keys();
				for (int i = 0; i < keysT.length; i++) {
					TIntObjectHashMap<Phrase> phrases = phrasesT.get(keysT[i]);
					int[] keys = phrases.keys();
					if (keys.length == 0)
						continue;			
					//different approach
					for (int j = 0; j < keys.length; j++) {
						Phrase p = phrases.get(keys[j]);
						if (p.getCounts() < _thresh) {
							phrases.remove(keys[j]);
							pt._numberOfPhrases--;
							int sourcePhraseId = p._sourcePhraseID;
							//TODO t._sourcePhraseSums[sourceLen][targetLen].put(sourcePhraseId,pt._sourcePhraseSums[sourceLen][targetLen].get(sourcePhraseId)-p.getCounts());
						}
					}
				}
			}
			int[] keys = pt._nullPhrases[targetLen].keys();
			for (int i = 0; i < keys.length; i++) {
				Phrase p = pt._nullPhrases[targetLen].get(keys[i]);
				if(p.getCounts() < _thresh){
					pt._nullPhrases[targetLen].remove(p._targetPhraseID);
				}
			}
		}
	}

}
